package service

import (
	"github.com/gogf/gf/v2/frame/g"
	"golang.org/x/net/context"
	"lot_admin_api/api"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	GenInt64Id "lot_admin_api/utility/genInt64IdOfString"
	"lot_admin_api/utility/paging"
	"lot_admin_api/utility/response"
)

type sNotice struct{}

var (
	insNotice = sNotice{}
)

func Notice() *sNotice {
	return &insNotice
}

// AddNotice  添加通知
func (s *sNotice) AddNotice(ctx context.Context, req *api.AddNoticeReq) (id string, err error) {
	req.Id = GenInt64Id.GenInt64IdOfString() // 生成雪花id
	//staffInfo := cache.StaffInfo(ctx)
	//req.CompanyId = staffInfo.CompanyId // 赋值公司id
	req.NoticeType = 2 // 固定通知类型
	//req.NoticeCreated = staffInfo.Id    // 赋值创建人id
	r := g.RequestFromCtx(ctx)
	if _, err := dao.LotNotice.Ctx(ctx).Save(req); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "保存通知失败")
	}
	return req.Id, err
}

// UpdateNotice 更新通知
func (s *sNotice) UpdateNotice(ctx context.Context, req *api.UpdateNoticeReq) (id string, err error) {
	_, err = s.SearchNotice(ctx, req.Id)
	//staffInfo := cache.StaffInfo(ctx)
	//req.CompanyId = staffInfo.CompanyId // 赋值公司id
	req.NoticeType = 2 // 固定通知类型
	//req.NoticeUpdated = staffInfo.Id    // 赋值创建人id
	r := g.RequestFromCtx(ctx)
	if _, err := dao.LotNotice.Ctx(ctx).OmitEmpty().Where("id", req.Id).Update(req); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "更新通知失败")
	}
	return req.Id, err
}

// DeleteNotice  删除通知
func (s *sNotice) DeleteNotice(ctx context.Context, req *api.DeleteNoticeReq) (id string, err error) {
	_, err = s.SearchNotice(ctx, req.Id)
	r := g.RequestFromCtx(ctx)
	if _, err = dao.LotNotice.Ctx(ctx).Where("id", req.Id).Delete(); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "删除失败")
	}
	return req.Id, err
}

// SearchNotice 判断有没有通知
func (s *sNotice) SearchNotice(ctx context.Context, Id string) (res *model.NoticeIdAndInfoRes, err error) {
	r := g.RequestFromCtx(ctx)
	err = dao.LotNotice.Ctx(ctx).Where("id", Id).Scan(&res)
	if res == nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "通知不存在")
	}
	return res, err
}

// NoticeList 获取通知列表
func (s *sNotice) NoticeList(ctx context.Context, req *api.NoticeListReq) (res *model.NoticeListRes, err error) {
	noticeList := make([]*model.NoticeIdAndInfoRes, 0)
	startNum, endNum := paging.Paging(req.Paging)
	//staffInfo := cache.StaffInfo(ctx)
	condition := g.Map{
		"notice_title like ?": "%" + req.NoticeTitle + "%", // 模糊查询通知标题
		"notice_created":      req.NoticeCreated,           // 创建人的id
		"notice_updated":      req.NoticeUpdated,           // 更新人的id
		"company_id":          req.CompanyId,               // 公司id
		"notice_top":          req.NoticeTop,               // 是否置顶
	}
	r := g.RequestFromCtx(ctx)
	// 获取通知列表
	if err = dao.LotNotice.Ctx(ctx).OmitEmpty().WithAll().Where(condition).OrderDesc("notice_top").OrderDesc("updated_at").Limit(startNum, endNum).Scan(&noticeList); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	// 获取总条数
	countInfo, err := dao.LotNotice.Ctx(ctx).OmitEmpty().Where(condition).Count()
	putCount := paging.PutCount(req.Paging, countInfo)
	res = &model.NoticeListRes{
		List:    noticeList,
		PutPage: *putCount,
	}
	return res, err
}
